package com.ql.service.zydj;

import com.ql.dao.zydj.ZydjMapper;
import com.ql.pojo.zydj.Zuyuandengji;
import com.ql.utils.PageHelper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("zydjService")
public class ZydjServiceImpl implements ZydjService {
    private static final Logger LOGGER = Logger.getLogger( ZydjServiceImpl.class );
    @Resource
    private ZydjMapper zydjMapper;

    //添加登记信息
    public boolean addPatient(Zuyuandengji patient) {
        Zuyuandengji zuyuandengji = new Zuyuandengji();
        try {
            int row = zydjMapper.addPatient( patient );
            if (row == 1) {
                System.out.println( "添加病人成功" );
                return true;
            } else {
                System.out.println( "添加病人失败" );
                return false;
            }
        } catch (Exception e) {
            System.out.println( "ZydjServiceImpl.addPatient异常" + e.getMessage() );
            return false;
        }
    }

    //查询住院病人
    public List<Zuyuandengji> qryAllPatient(Date qrycheckInTime, int userId, String qryUserName, String qryKeshi, PageHelper ph) {
        List<Zuyuandengji> zuyuandengjiList = null;
        try {
            ph.setTotalCount( zydjMapper.qryCoutPatient(qrycheckInTime,userId,qryUserName,qryKeshi) );
            zuyuandengjiList = zydjMapper.qryAllPatient(qrycheckInTime,userId,qryUserName,qryKeshi,(ph.getCurrentPageNo() - 1) * ph.getPageSize(), ph.getPageSize() );
            LOGGER.info( "查询住院病人成功" );
        } catch (Exception e) {
            LOGGER.error( "ZydjServiceImpl.qryAllPatient异常" + e.getMessage() );
        }
        return zuyuandengjiList;
    }


//    根据id查询病人
    public Zuyuandengji qryById(int userId) {
        Zuyuandengji zuyuandengji = null;
        try {
            zuyuandengji = zydjMapper.qryByUserId( userId );
            LOGGER.info( "查询病人详情成功" );
        } catch (Exception e) {
            LOGGER.error( "ZydjServiceImpl.qryById异常!" + e.getMessage() );
        }
        return zuyuandengji;
    }

    @Override
    public boolean qryId(int userId) {
        Zuyuandengji zuyuandengji = null;
        try {
            zuyuandengji = zydjMapper.qryByUserId( userId );
            if (null==zuyuandengji){//该住院号未住院，可以使用
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error( "ZydjServiceImpl.qryId异常" + e.getMessage() );
            return false;
        }
    }
//病人转科
    @Override
    public boolean updKeshi(Zuyuandengji zuyuandengji) {
        int row = 0 ;
        try {
            row = zydjMapper.updKeshi( zuyuandengji );
            if (row==1){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error( "ZydjServiceImpl.qryId异常"+e.getMessage() );
            return false;
        }

    }

//   病人出院
    @Override
    public boolean outHospital(Zuyuandengji zuyuandengji) {
        int row  = 0;
        try {
            row = zydjMapper.OutHospital( zuyuandengji );
            if (row==1){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error( "ZydjServiceImpl.outHospital异常"+e.getMessage());
        return false;
        }
    }
}
